import { useUserInfo, useUserActions } from '@/store'
import { routes } from '@/router/constant'
import { setItem } from '@/utils/storage'

import { UserType } from '#/store'
import { StorageEnum } from '#/enum'
import type { RouteConfig } from '#/router'

const loginRequest = (): Promise<RouteConfig[]> => {
  console.log('获取数据')
  return new Promise(res => {
    setTimeout(() => {
      res(routes)
    }, 1000)
  })
}

export default function Login() {
  const { setUserInfo } = useUserActions()
  const { accessToken } = useUserInfo()

  const login = useCallback(async (data: UserType) => {
    const res: RouteConfig[] = await loginRequest()
    setUserInfo({ ...data, permissions: res })
    setItem(StorageEnum.User, data)
  }, [])

  if (accessToken) {
    return <Navigate to='/' replace></Navigate>
  }

  return (
    <div className='flex-center'>
      <h1>login</h1>
      <Button
        type='primary'
        onClick={() => login({ useName: '80800', id: '456464', permissions: [], accessToken: '446', refreshToken: 'sdafasf' })}
      >
        Button
      </Button>
    </div>
  )
}
